<template>
	<view clas="notice">
		<!--导航栏-->
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="tabCurrentIndex === index ? `text-${themeColor.name} current` : ''" @tap="tabClick(index)">
				{{ item.text }}
			</view>
		</view>
		<block v-if="tabCurrentIndex === 0">
			<view class="notice-item" v-for="item in announceList" :key="item.id">
				<!-- <text class="time">{{ item.createTime | time }}</text> -->
				<view class="content" @tap="navTo(`/pages/index/notice/detail?id=${item.id}&type=1`)">
					<text class="title">{{ item.noticeTopic }}</text>
					<view class="img-wrapper">
						<rf-image class="pic" :preview="false" :mode="'aspectFit'" :src="item.imgPath ? baseUrl + '/upload/' + item.imgPath : errorImg"></rf-image>
					</view>
					<text class="introduce" v-if="false">{{ item.synopsis }}</text>
					<view class="bot b-t">
						<text>查看详情</text>
						<text>{{ item.createTime | time }}</text>

						<i class="more-icon iconfont iconyou"></i>
					</view>
				</view>
			</view>
		</block>
		<block v-else-if="tabCurrentIndex === 1">
			<view class="notice-item" v-for="item in announceList" :key="item.id">
				<!-- <text class="time">{{ item.createTime | time }}</text> -->
				<view class="content" @tap="navTo(`/pages/index/notice/detail?id=${item.id}&type=2`)">
					<text class="title">{{ item.infoTitle }}</text>
					<view class="img-wrapper">
						<rf-image class="pic" :preview="false" :mode="'aspectFit'" :src="item.imgPath ? baseUrl + '/upload/' + item.imgPath : errorImg"></rf-image>
					</view>
					<text class="introduce" v-if="false">{{ item.synopsis }}</text>
					<view class="bot b-t">
						<text>查看详情</text>
						<text>{{ item.createTime | time }}</text>

						<i class="more-icon iconfont iconyou"></i>
					</view>
				</view>
			</view>
		</block>
		<block v-else>
			<view class="notice-item" v-for="item in announceList" :key="item.id">
				<!-- <text class="time">{{ item.createTime | time }}</text> -->
				<view class="content" @tap="navTo(`/pages/index/notice/detail?id=${item.id}&type=3`)">
					<text class="title">{{ item.activityName }}</text>
					<view class="img-wrapper">
						<rf-image class="pic" :preview="false" :mode="'aspectFit'" :src="item.activityPic ? baseUrl + '/upload/' + item.activityPic : errorImg"></rf-image>
					</view>
					<text class="introduce" v-if="item.infoText">{{ item.infoText }}</text>
					<view class="bot b-t">
						<text>查看详情</text>
						<text>{{ item.holdTime }}</text>

						<i class="more-icon iconfont iconyou"></i>
					</view>
				</view>
			</view>
		</block>

		<rf-load-more class="load-more" :status="loadingType" v-if="announceList.length > 0"></rf-load-more>
		<rf-empty info="暂无商城公告" v-if="announceList.length === 0 && !loading"></rf-empty>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>
</template>

<script>
import { notifyAnnounceIndex, getMyNewsList, getMyActivityList } from '@/api/basic';
import rfLoadMore from '@/components/rf-load-more/rf-load-more.vue';
import moment from '@/common/moment';
export default {
	components: { rfLoadMore },
	data() {
		return {
			announceList: [], // 公告列表
			loadingType: 'more',
			loading: true,
			page: 1,
			userInfo: {},
			baseUrl: this.$mConfig.baseUrl,
			errorImg: this.$mAssetsPath.errorImage,
			tabCurrentIndex: 0,
			navList: [{ state: 0, text: '通知公告' }, { state: 1, text: '新闻资讯' }, { state: 2, text: '活动讯息' }]
		};
	},
	filters: {
		// 时间格式化
		time(val) {
			return val.substring(0, val.length - 2);
		}

	},
	// 下拉刷新
	onPullDownRefresh() {
		this.page = 1;
		this.announceList.length = 0;
		this.getNotifyAnnounceIndex('refresh');
	},
	// 加载更多
	onReachBottom() {
		if (this.loadingType === 'nomore') return;
		this.page++;
		this.getNotifyAnnounceIndex();
	},
	onLoad() {
		this.userInfo = uni.getStorageSync('userInfo');
		this.initData();
	},
	methods: {
		tabClick(index) {
			this.tabCurrentIndex = index;
			this.page = 1;
			this.announceList.length = 0;
			this.getNotifyAnnounceIndex();
		},
		// 数据初始化
		initData() {
			this.getNotifyAnnounceIndex();
		},
		// 获取通知列表
		async getNotifyAnnounceIndex(type) {
			let corpId = 10;
			if (this.userInfo.corpId) corpId = this.userInfo.corpId;
			let url = `${notifyAnnounceIndex}`;
			if (this.tabCurrentIndex == 1) {
				url = `${getMyNewsList}`;
			} else if (this.tabCurrentIndex == 2) {
				url = `${getMyActivityList}`;
			}
			await this.$http
				.get(url, { page: this.page, corpId: corpId })
				.then(r => {
					this.loading = false;
					if (type === 'refresh') {
						uni.stopPullDownRefresh();
					}
					this.loadingType = r.data.length === 10 ? 'more' : 'nomore';
					this.announceList = [...this.announceList, ...r.data];
				})
				.catch(() => {
					if (type === 'refresh') {
						uni.stopPullDownRefresh();
					}
					this.loading = false;
				});
		},
		navTo(route) {
			this.$mRouter.push({ route });
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $page-color-base;
	padding-bottom: 30upx;
}

.notice-item {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.time {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80upx;
	padding-top: 10upx;
	font-size: 26upx;
	color: #7d7d7d;
}

.content {
	width: 710upx;
	padding: 0 24upx;
	background-color: #fff;
	border-radius: 4upx;
}

.title {
	display: flex;
	align-items: center;
	height: 90upx;
	font-size: 32upx;
	color: #303133;
}

.img-wrapper {
	width: 100%;
	height: 260upx;
	position: relative;
}

.pic {
	display: block;
	width: 100%;
	height: 100%;
	border-radius: 6upx;
}

.cover {
	display: flex;
	justify-content: center;
	align-items: center;
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	font-size: 36upx;
	color: #fff;
}

.introduce {
	display: inline-block;
	padding: 16upx 0;
	font-size: 28upx;
	color: #606266;
	line-height: 38upx;
}

.bot {
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 80upx;
	font-size: 24upx;
	color: #707070;
	position: relative;
}

.more-icon {
	font-size: 32upx;
}
</style>
